/**
 * Created by zjp on 2016/9/24.
 */


var User = function (target,addDlg,editDlg,tb,url,demain,dbfun) {
    this.dg=target;
    this.addDlg=addDlg;
    this.editDlg=editDlg;
   var r=new $.RestClient('',{stringifyData:true});

    this.dataRest=r.add(demain);
    $(this.dg).datagrid({
        onDblClickRow:dbfun,
        pagination:true,
        loadFilter:function (data) {
            var d={};
            d.rows=data.content;
            d.total=data.totalElements
            return d;
        },
        onBeforeLoad:function(param) {

        $.extend(true,param,$(tb+ ' form').serializeJSON());
        param.page=param.page-1;
        param.size=param.rows;
        return param;
    },
        rownumbers:true,
        toolbar:tb,method:'get',url:url
    });
};
User.prototype.create = function () {
   $(this.addDlg).dialog({
       closed:false
   })

}
User.prototype.deleteUser = function () {
    var dg=this.dg;
    var id=$(dg).datagrid('getSelected').id;
    this.dataRest.del(id).done(function () {
        $(dg).datagrid('reload');
    })

}
User.prototype.search = function () { var dg=this.dg;
    $(dg).datagrid('reload');
};
User.prototype.save=function (data) {
    var dg=this.dg;
    this.dataRest.create(data).done(function (d) {
        $(dg).datagrid('reload');
    });
};
User.prototype.update=function (data) {  var dg=this.dg;
   data= $( this.editDlg+' form').serializeJSON();
    this.dataRest.update(data.id, data).done(function (d) {
        $(dg).datagrid('reload');
    });
};
